import * as Cesium from 'cesium'

interface SkyboxSource {
  positiveX: string
  negativeX: string
  positiveY: string
  negativeY: string
  positiveZ: string
  negativeZ: string
}

export default function useSkyBox(backgroundSource: SkyboxSource, groundSource?: SkyboxSource) {
  window.viewer.scene.skyBox = new Cesium.SkyBox({
    sources: backgroundSource
  })

  const defaultSkyBox = window.viewer.scene.skyBox
  const currentSkybox = new Cesium.SkyBox({ sources: groundSource })
  window.viewer.scene.postRender.addEventListener(() => {
    const position = window.viewer.camera.position
    if (Cesium.Cartographic.fromCartesian(position).height < 225705) {
      window.viewer.scene.skyBox = currentSkybox
      window.viewer.scene.skyAtmosphere.show = false
    } else {
      window.viewer.scene.skyBox = defaultSkyBox
      window.viewer.scene.skyAtmosphere.show = true
    }
  })
}
